package com.text.jwt.services.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.text.jwt.entities.dto.Account;
import com.text.jwt.mapper.AccountMapper;
import com.text.jwt.services.AccountService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService{

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
        Account account = this.findAccountByName(username);
        // 还需要判断以下密码是否正确
        if (account == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        System.out.println("查找到的用户: "+account);
        return User
                .withUsername(username)
                .password(account.getPassword())
                .roles(account.getRole())
                .build();
    }

    public Account findAccountByName(String username) {
        return this.query()
                .eq("username", username)
                .one();
    }
}
